using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PacMan.Components;

namespace MiniPac.Components
{
	class Stage
	{
		private Maze mMaze;
		public Maze maze { get { return mMaze; } }

		private GState prevState;

		public int SizeX { get { return mMaze.MazePixelSizeX + 2 * R.MarginX; } }
		public int SizeY { get { return mMaze.MazePixelSizeY + 2 * R.MarginY; } }

		private Player player;

		private Ghost Blinky;
		private Ghost Pinky;
		private Ghost Inky;
		private Ghost Clyde;

		//Screens//
		public Screen_NewGame New_Screen;
		public Screen_PauseGame PAUSE_Screen;
		public Screen_EndGame END_Screen;

		public static bool Scared = false;

		public Stage()
		{
			mMaze = new Maze(R.MarginX, R.MarginY);
			player = (new Player(mMaze, 0, 0, Color.Yellow));
			player.ResetPos();
			Blinky = new Ghost(player, GType.B, maze);
			Pinky = new Ghost(player, GType.P, maze);
			Inky = new Ghost(player, GType.I, maze);
			Clyde = new Ghost(player, GType.C, maze);

			New_Screen = new Screen_NewGame();
			PAUSE_Screen = new Screen_PauseGame();
			END_Screen = new Screen_EndGame();

			Debug.WriteLine(END_Screen.hs.ToString());
			END_Screen.hs.Save();

			prevState = GState.Playing;

			
		}

		public void Draw(SpriteBatch sb)
		{
			switch (State.gameState)
			{
				case GState.New:
					//New_Screen.Draw(sb);
					break;
				case GState.Paused:
					DrawMaze(sb);
					PAUSE_Screen.Draw(sb);
					break;
				case GState.End:
					//END_Screen.Draw(sb);
					break;
				default:
					DrawMaze(sb);
					break;
			}
		}

		public void Update(Double time)
		{
			//Debug.WriteLine(State.gameState.ToString());
			//Debug.WriteLine(" pr " + prevState.ToString());

			if (InputHandler.UpdateInput(time).p)
			{
				if (State.gameState != GState.Paused)
				{
					prevState = State.gameState;
					State.gameState = GState.Paused;
					return;
				}

				if (State.gameState == GState.Paused)
				{
					if (InputHandler.UpdateInput(time).p)
						State.gameState = prevState;
				}
			}

			if (State.gameState == GState.Playing)
			{
				State.tick();

				player.Update(time);
				Blinky.Update(time);
				Pinky.Update(time);
				Inky.Update(time);
				Clyde.Update(time);
			}
		}

		private void DrawMaze(SpriteBatch sb)
		{
			sb.DrawString(R.c64_font, "Score: " + State.pnts , new Vector2(R.StageSizeX, R.MarginY), Color.PowderBlue);
			sb.DrawString(R.c64_font, "Lives: " + State.lives, new Vector2(R.StageSizeX, R.MarginY + 15), Color.PowderBlue);

			mMaze.Draw(sb);
			player.Draw(sb);

			Blinky.Draw(sb);
			Pinky.Draw(sb);
			Inky.Draw(sb);
			Clyde.Draw(sb);
		}
	}
}
